/**
 * 
 */
package application;

import game.GameDatabase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import technicalservices.database.FileHandler;
import technicalservices.database.PersistanseStorage;

import domain.league.ILeague;
import domain.match.GameMatch;
import domain.tournament.DeathMatch;
import domain.tournament.FreePlay;
import domain.tournament.ITournament;
import domain.tournament.Tournament;
import domain.users.User;

/**
 * Controller for all the leagues.
 *
 */
public class LeagueController {
	
	private List<ITournament> tournaments;
	private ITournament tournament;
	private FileHandler fh;
	
	public LeagueController() {
		fh = new FileHandler("Tournaments.dat");
		tournaments = (List<ITournament>) fh.loadFile();
	}
	
	private void initTournament() {
		tournaments = PersistanseStorage.getInstance().getTournaments();
	}
	private void saveTournament() {
		PersistanseStorage.getInstance().saveTournaments(tournaments);
	}
	
	/*
	 * This method get all the tournaments for a specific League owner.
	 * @returns List<ITournament>
	 */
	public List<ITournament> getLeagues(int gameID, User leagueOwner) {
		List<ILeague> leagues = GameDatabase.getInstance().getGameDatabase(gameID);

		for (ILeague iLeague : leagues) {
			if(iLeague.getUser().getUserId() == leagueOwner.getUserId())
				tournaments = iLeague.getTournaments();
				return tournaments;
		}
		return null;
	}
	
	
	public List<User> getAdvertiser() {
		return PersistanseStorage.getInstance().getUsers("Advertiser");
	}
	
	public void solictSponsorship(User advertiser) {
		tournament.setAdvertiser(advertiser);
	}
	
	public void announceTournament() {
		tournaments.add(tournament);
		saveTournament();
	}
	/*
	 * This method is for applying for a specific tournament.
	 */
	public boolean applyForTournament(ITournament iTournament,User player) {
		if (tournaments.get(iTournament.getTournamentID()).getNumPlayers() < 64) {
			tournaments.get(iTournament.getTournamentID()).addPlayer(player);
			return true;
		}
		return false;		
	}

	public void createNewTournament(String tournamentName, int numPlayers, String style, Date duration) {
		if(style == "FreePlay") {
			// TODO: Add number of players? and Tournament end date?
			tournament = new FreePlay(tournaments.size(), tournamentName, ArenaManager.getInstance().getUser(), numPlayers);
		} else if(style == "DeathMatch") {
			tournament = new DeathMatch(tournaments.size(), tournamentName, ArenaManager.getInstance().getUser(), numPlayers);	
		}
		//announceTournament();
		
	}

	public List<ITournament> getTournaments(User user) {
		/*for (ITournament iTournament : tournaments) {
			
		}*/
		return tournaments;
	}
	
	public void archiveTournament(ITournament tournament) {
		for(int i=0;i<tournaments.size();i++) {
			if(tournaments.get(i).getTournamentID() == tournament.getTournamentID()) {
				PersistanseStorage.getInstance().archiveTournament(tournaments.remove(i));
			}
		}		
	}
	
	public void conductTournament(ITournament tournament) {
		tournament.startGame();
	}
}
